package com.stylefeng.guns.modular.system.jqgrid;

import java.util.Arrays;
import java.util.Iterator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;

/**
 * 用来接收页面传过来的查询字段   对象
 * 
 * @author Greedwind
 * 
 */
public class FrontPage<T>{
	//是否是查询
	private boolean _search;
	
	//时间戳（毫秒）
	private String nd;
	
	//每页显示条数
	private int rows;
	
	//当前页数
	private int page;
	
	//排序的字段
	private String sidx;
	
	//排序方式 asc升序  desc降序
	private String sord;
	
	//搜索条件
	//filters
	private String filters;

	public String getFilters() {
		return filters;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

	public boolean is_search() {
		return _search;
	}

	public void set_search(boolean _search) {
		this._search = _search;
	}

	public String getNd() {
		return nd;
	}

	public void setNd(String nd) {
		this.nd = nd;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}
	
	//获取mybatisPlus封装的Page对象
	public Page<T> getPagePlus(){
		Page<T> pagePlus = new Page<T>();
		pagePlus.setCurrent(this.page);
		pagePlus.setSize(this.rows);
		pagePlus.setAsc(this.sord==null?true:this.sord.equals("asc"));
		//驼峰转下划线
		pagePlus.setOrderByField(StringUtils.camelToUnderline(this.sidx));
		
		return pagePlus;
	}
	
	public Wrapper<T> getWrapper(){
		Wrapper<T> wrapper = new EntityWrapper<T>();
		String filters = this.getFilters();
		if(filters!=null && filters!="") {
			JSONObject jsonFilter = JSONObject.parseObject(filters);
			String groupOp = jsonFilter.getString("groupOp");
			JSONArray rules = jsonFilter.getJSONArray("rules");
			for (Iterator iterator = rules.iterator();iterator.hasNext();){
				JSONObject rule = (JSONObject) iterator.next();
				String op = rule.getString("op");
				if (groupOp.toUpperCase().equals("AND")){ 
					wrapper.and();
				} else {
					wrapper.or();
				}
				if ("eq".equals(op)) {
					wrapper.eq(StringUtils.camelToUnderline(StringUtils.camelToUnderline(rule.getString("field"))), rule.getString("data"));
				} else if ("ne".equals(op)) {
					wrapper.ne(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"));
				} else if ("lt".equals(op)) {
					wrapper.lt(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"));
				} else if ("le".equals(op)) {
					wrapper.le(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"));
				} else if ("gt".equals(op)) {
					wrapper.gt(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"));
				} else if ("ge".equals(op)) {
					wrapper.ge(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"));
				} else if ("bw".equals(op)) { //'begins with
					wrapper.like(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.RIGHT);
				} else if ("bn".equals(op)) { //does not begin with
					wrapper.notLike(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.RIGHT);
				} else if ("in".equals(op)) { //is in
					String[] inList = rule.getString("data").split(",");
					wrapper.in(StringUtils.camelToUnderline(rule.getString("field")), Arrays.asList(inList));
				} else if ("ni".equals(op)) { //is not in
					String[] inList = rule.getString("data").split(",");
					wrapper.notIn(StringUtils.camelToUnderline(rule.getString("field")), Arrays.asList(inList));
				} else if ("ew".equals(op)) { //ends with
					wrapper.like(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.LEFT);
				} else if ("en".equals(op)) { //does not end with
					wrapper.notLike(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.LEFT);
				} else if ("cn".equals(op)) { //contains jqGrid文本搜索默认操作符
					wrapper.like(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.DEFAULT);
				} else if ("nc".equals(op)) { //not contains
					wrapper.notLike(StringUtils.camelToUnderline(rule.getString("field")), rule.getString("data"), SqlLike.DEFAULT);
				}

			}
		}		
		return wrapper;
	}
}